#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QFileDialog>
#include <QMessageBox>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow) ,
    view(new MyGraphicsView()),
    scene(new QGraphicsScene())
{
    ui->setupUi(this);
    this->showMaximized();

    this->setCentralWidget(this->view);
    this->view->setAlignment(Qt::AlignLeft|Qt::AlignTop);

    //Conectando o sinal de movimento do mouse
    QObject::connect(view, SIGNAL(mouseMoved(QPointF)), this, SLOT(setMousePositionOnLabel(QPointF)));

}

MainWindow::~MainWindow()
{
    delete ui;
    delete this->view;
    delete this->scene;
}

void MainWindow::on_actionAbrir_triggered()
{
    QString fileName = QFileDialog::getOpenFileName(this, tr("Abrir Imagem"),QDir::homePath(), tr("Imagens (*.png *.xpm *.jpg *.bmp)"));
    if (!fileName.isEmpty())
    {
        QImage image(fileName);
        if (image.isNull())
        {
            QMessageBox::information(this, this->windowTitle(), tr("Impossível carregar %1.").arg(fileName));
            return;
        }

        this->scene->clear();
        this->scene->addPixmap(QPixmap::fromImage(image));

        this->view->setScene(this->scene);
    }

}

void MainWindow::on_actionSair_triggered()
{
    this->close();
}

void MainWindow::on_actionSobre_triggered()
{
    QMessageBox::about(this, tr("Sobre"),
            tr("<p>Software do Laboratorio de Metrologia - Ensaio de Dureza</p>"
               "<p>Engenharia Mecanica - UFMG</p>"));
}

void MainWindow::setMousePositionOnLabel(QPointF point)
{
    //std::string text = "(" + /*point.x()*/ + ", " /*+ point.y()*/;
    QString text;
    QString xValue, yValue;

    xValue.setNum((int)point.x());
    yValue.setNum((int)point.y());

    text.append("(").append(xValue).append(", ").append(yValue).append(")");

    this->ui->statusBar->showMessage(text);
}
